package org.phalanx.client.mapeditor.dialogs;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.phalanx.client.UI.DialogManager;
import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.PhalanxButtonFactory;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.mapeditor.PhalanxMapEditorPanel;
import org.phalanx.client.mapeditor.PhalanxMapMetadataPanel;
import org.phalanx.client.mapeditor.PhalanxMapNameInfoPanel;
import org.phalanx.map.PhalanxMap;

public class PhalanxMapInfoDialog extends JFrame implements pButtonListener{
	private static final long serialVersionUID = 8521408668878829487L;
	public static final String NAME = "MAPINFODIALOG";
	private PhalanxMapEditorPanel masterPanel;
	private JPanel contextPanel;
	private PhalanxMapNameInfoPanel namePanel;
	private PhalanxMapMetadataPanel metaPanel;
	private PhalanxIconButton okButton;
	private PhalanxIconButton cancelButton;
	
	public PhalanxMapInfoDialog(PhalanxMapEditorPanel parentPanel) {
		masterPanel = parentPanel;
		initializeFrame();
		getExternalPanels();
		setUpButtons();
		populate();
		
		addNamePanelGrid();
		addMetaPanelGrid();
		addButtonPanel();
		this.setContentPane(contextPanel);
	}
	
	private void addNamePanelGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 0.5;
		c.weightx = 0.5;
		
		TitledBorder t = BorderFactory.createTitledBorder("Name and Version");
		t.setTitleColor(Color.white);
		
		namePanel.setBorder(t);
		
		contextPanel.add(namePanel, c);
	}
	
	private void addMetaPanelGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 0.5;
		c.weightx = 0.5;
		
		TitledBorder t = BorderFactory.createTitledBorder("Author Information");
		t.setTitleColor(Color.white);
		
		metaPanel.setBorder(t);
		
		contextPanel.add(metaPanel, c);
	}

	private void setUpButtons() {
		okButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_OK_BUTTON);

		cancelButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_CANCEL_BUTTON);
		
		okButton.addButtonListener(this);
		cancelButton.addButtonListener(this);
	}

	private void addButtonPanel() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.fill = GridBagConstraints.HORIZONTAL;

		JPanel newPanel = new JPanel();
		newPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		newPanel.setBackground(Color.black);
		
		newPanel.add(okButton);
		newPanel.add(cancelButton);
		
		contextPanel.add(newPanel, c);
		
	}
	
	private void initializeFrame() {
		contextPanel = new JPanel();
		contextPanel.setLayout(new GridBagLayout());
		contextPanel.setBackground(Color.black);
		this.setSize(500, 500);
		this.setTitle("Map Properties");
		
		JFrame masterFrame = masterPanel.getMasterFrame();
		int pX = ((masterFrame.getWidth() + masterFrame.getLocation().x) + 
				masterFrame.getLocation().x) / 5;
		int pY = ((masterFrame.getHeight() + masterFrame.getLocation().y) + 
				masterFrame.getLocation().y) / 5;
		
		this.setLocation(pX, pY);
	}
	
	private void getExternalPanels() {
		namePanel = new PhalanxMapNameInfoPanel();
		metaPanel = new PhalanxMapMetadataPanel();
		
		metaPanel.getAuthorURL().setColumns(20);
	}
	
	private void populate() {
		PhalanxMap currentMap = masterPanel.getDisplayContainer().getMapViewObject().getMap(); 
		namePanel.setMapName(currentMap.getMapName());
		namePanel.setMapVersion(currentMap.getMapVersion());
		metaPanel.setAuthorName(currentMap.getAuthorName());
		metaPanel.setAuthorEmail(currentMap.getAuthorEmail());
		metaPanel.setAuthorCountry(currentMap.getAuthorCountry());
		metaPanel.setAuthorURL(currentMap.getAuthorURL());
	}
	
	private void closeWindow() {
		DialogManager d = DialogManager.getInstance();

		d.removeDialog(NAME);

		this.dispose();
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent().equals(okButton)) {
			populateMap();
			this.closeWindow();
		}
		else if (p.getComponent().equals(cancelButton)) {
			this.closeWindow();
		}
		
	}
	
	private void populateMap() {
		PhalanxMap currentMap = masterPanel.getDisplayContainer().getMapViewObject().getMap();
		
		currentMap.setMapName(namePanel.getMapName().getText());
		currentMap.setMapVersion(namePanel.getMapVersion().getText());
		currentMap.setAuthorName(metaPanel.getAuthorName().getText());
		currentMap.setAuthorEmail(metaPanel.getAuthorEmail().getText());
		currentMap.setAuthorCountry(metaPanel.getAuthorCountry().getText());
		currentMap.setAuthorURL(metaPanel.getAuthorURL().getText());
	}
}
